package mediator 
{
	import core.Mediator;
	import data.Chapter;
	import event.LessonEvent;
	import event.PlayerEvent;
	import event.PositionEvent;
	import view.MediaPlayerView;
	
	/**
	 * ...
	 * @author liwei
	 */
	public class MediaPlayerMediator extends Mediator 
	{
		[Inject]
		public var _view:MediaPlayerView;
		public function MediaPlayerMediator() 
		{
			super();
			
		}
		override public function onRegister():void 
		{
			addActorListener(LessonEvent.CHANGE, change);
			addActorListener(PositionEvent.PLAY, play);
			addActorListener(PositionEvent.PAUSE, pause);
			addActorListener(PositionEvent.SET_POSITION, setPosition);
			addActorListener(PositionEvent.COMPLETE_POSITION, completePosition);
			addActorListener(LessonEvent.NEXT, next);
			addActorListener(LessonEvent.PREV, prev);
			_view.addEventListener(LessonEvent.NEXT, next);
			addActorListener(PlayerEvent.HIDE_MENU, hideMenu);
			addActorListener(PlayerEvent.SHOW_MENU, showMenu);
		}
		private function showMenu(e:PlayerEvent):void 
		{
			_view.showMenu();
		}
		
		private function hideMenu(e:PlayerEvent):void 
		{
			_view.hideMenu();
		}
		
		private function prev(e:LessonEvent):void 
		{
			if (State.showMenu) return;
			if (State.chapter < 0) {
				//第一章
				return;
			}
			var chap:Chapter = Chapter.getChapter(State.chapter);
			if (State.lesson - 1 < 0) {
				State.chapter--;
				chap = Chapter.getChapter(State.chapter);
				if (chap) {
					State.lesson = chap.length - 1;
				}
			}else {
				State.lesson--;
			}
			changeLocation();
		}
		
		private function next(e:LessonEvent):void 
		{
			if (State.showMenu) return;
			if (State.chapter >= Chapter.getChapters().length) {
				//最后一章
				return;
			}
			var chap:Chapter = Chapter.getChapter(State.chapter);
			if (State.lesson + 1 >= chap.length) {
				State.lesson = 0;
				State.chapter++;
			}else {
				State.lesson++;
			}
			if (State.chapter >= Chapter.getChapters().length) {
				State.chapter--;
				_view.stop();
				return;
			}
			changeLocation();
		}
		private function changeLocation():void {
			if (State.showMenu) return;
			if (State.chapter < 0) {
				State.chapter = 0;
			}
			if (State.chapter > Chapter.getChapters().length - 1) {
				State.chapter = Chapter.getChapters().length - 1;
				return;
			}
			_view.load(State.chapter, State.lesson);
		}
		
		private function completePosition(e:PositionEvent):void 
		{
			if (State.showMenu) return;
			_view.finishPostition();
		}
		
		private function setPosition(e:PositionEvent):void 
		{
			if (State.showMenu) return;
			_view.setPosition(e.p);
		}
		
		private function play(e:PositionEvent):void 
		{
			if (State.showMenu) return;
			_view.play();
		}
		
		private function pause(e:PositionEvent):void 
		{
			if (State.showMenu) return;
			_view.stop();
		}
		
		private function change(e:LessonEvent):void 
		{
			_view.load(e.chapter, e.lesson);
			State.chapter = e.chapter;
			State.lesson = e.lesson;
			_view.hideMenu();
		}
	}

}